// HoverButton.cpp : implementation file
//

#include "stdafx.h"
#include "Check.h"
#include "HoverButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHoverButton

CHoverButton::CHoverButton()
{
	m_nButtonState=BUTTON_OFF;
	m_bMouseTracking = FALSE;
}

CHoverButton::~CHoverButton()
{
}


BEGIN_MESSAGE_MAP(CHoverButton, CButton)
	//{{AFX_MSG_MAP(CHoverButton)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHoverButton message handlers

BUTTON_STATE CHoverButton::SetButtonState(BUTTON_STATE nState)
{
	BUTTON_STATE nOldState=(BUTTON_STATE)GetCheck();

	m_nButtonState=nState;
	switch(m_nButtonState)
	{
	case BUTTON_ON:
		EnableWindow(TRUE);
		SetState(BUTTON_ON);
		break;
	case BUTTON_OVER:
		EnableWindow(TRUE);
		SetState(BUTTON_OVER);
		break;
	case BUTTON_GREYED:
		EnableWindow(FALSE);
		break;
	default:
		EnableWindow(TRUE);
		SetState(BUTTON_OFF);
		m_nButtonState=BUTTON_OFF;
		break;
	}

	return nOldState;
}

void CHoverButton::OnMouseEnter()
{
	m_bMouseTracking=TRUE;
	m_nButtonState=BUTTON_OVER;
	
	SetCapture();
	Invalidate();
	UpdateWindow();
}

void CHoverButton::OnMouseLeave()
{
	m_bMouseTracking=FALSE;
	m_nButtonState=BUTTON_OFF;

	Invalidate();
	UpdateWindow();
	ReleaseCapture();
}

void CHoverButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	CButton::OnMouseMove(nFlags, point);

	if((!m_bMouseTracking||GetCapture()!=this)&&(m_nButtonState==BUTTON_OFF))
	{
		OnMouseEnter();
	}
	else
	{
		if(m_nButtonState==BUTTON_OVER)
		{
			CRect rc;
			GetClientRect(&rc);
			if(!rc.PtInRect(point))
				OnMouseLeave();
		}
	}
}

//void CHoverButton::OnLButtonUp(UINT nFlags, CPoint point) 
//{
//	SetButtonState(BUTTON_ON);
//	CButton::OnLButtonUp(nFlags, point);
//}

void CHoverButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CDC*		pDC;
	CDC			memDC;
	int			iSaveDC;
	CBitmap*	pBitmap=NULL;
	CBitmap*	pOld=NULL;
	CRect		rc;

	pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
	memDC.CreateCompatibleDC(pDC);
	VERIFY(pDC);
	iSaveDC=pDC->SaveDC();
	rc.CopyRect(&lpDrawItemStruct->rcItem);
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(GetSysColor(COLOR_WINDOWFRAME));

	switch(m_nButtonState)
	{
	case BUTTON_ON:
		pBitmap=&m_bmpButtonDown;
		break;
	case BUTTON_OVER:
		pBitmap=&m_bmpButtonFocussed;
		break;
	case BUTTON_GREYED:
		pBitmap=&m_bmpButtonDisabled;
		break;
	default:
		pBitmap=&m_bmpButtonUp;
		break;
	}

	CString		strTitle;
	GetWindowText(strTitle);

	if (pBitmap->m_hObject)
	{
		CRect	rcBitmap(rc);
		BITMAP	bmpInfo;			
		CSize	size;

		// Text
		size = pDC->GetTextExtent(strTitle);
		rcBitmap.OffsetRect(size.cx+5,0);

		// Draw bitmap
		if(!pBitmap->GetBitmap(&bmpInfo))
			return;
		pOld=memDC.SelectObject((CBitmap*) pBitmap);
		if (pOld==NULL) 
			return; //Destructors will clean up

		if(!pDC->BitBlt(0, 0, rc.Width(), rc.Height(), &memDC, 0, 0, SRCCOPY))
			return;
		memDC.SelectObject(pOld);	
		if(memDC==NULL)
			return;
	}

	CRect	rcText(rc);
	UINT nFormat = DT_CENTER;

	if(m_nButtonState == BUTTON_GREYED)
	{
		rcText.OffsetRect(1,1);
		pDC->SetTextColor(GetSysColor(COLOR_BTNHIGHLIGHT));
		pDC->DrawText(strTitle,rcText,nFormat);
		rcText.OffsetRect(-1,-1);
		pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
		pDC->DrawText(strTitle,rcText,nFormat);
	} 
	else
		pDC->DrawText(strTitle,rcText,nFormat);

	pDC->RestoreDC(iSaveDC);
}

BOOL CHoverButton::LoadBitmaps(UINT nBitmapUp, UINT nBitmapDown, 
							   UINT nBitmapFocus, UINT nBitmapDisabled)
{
	return LoadBitmaps(MAKEINTRESOURCE(nBitmapUp),
						MAKEINTRESOURCE(nBitmapDown),
						MAKEINTRESOURCE(nBitmapFocus),
						MAKEINTRESOURCE(nBitmapDisabled));
}

BOOL CHoverButton::LoadBitmaps(LPCSTR lpszBitmapUp, LPCSTR lpszBitmapDown, 
							   LPCSTR lpszBitmapFocus, LPCSTR lpszBitmapDisabled)
{
	BOOL bAllLoaded=TRUE;

	//Delete old ones
	m_bmpButtonDown.DeleteObject();
	m_bmpButtonFocussed.DeleteObject();
	m_bmpButtonUp.DeleteObject();
	m_bmpButtonDisabled.DeleteObject();
	
	if (!m_bmpButtonUp.LoadBitmap(lpszBitmapUp))
	{
		TRACE0("Failed to load up bitmap of bitmap button\n");
		return FALSE;
	}

	if (lpszBitmapDown!=NULL)
	{
		if (!m_bmpButtonDown.LoadBitmap(lpszBitmapDown))
		{
			TRACE0("Failed to load down bitmap of bitmap button\n");
			return bAllLoaded=FALSE;
		}
	}
	
	if (lpszBitmapFocus!=NULL)
	{
		if (!m_bmpButtonFocussed.LoadBitmap(lpszBitmapFocus))
		{
			TRACE0("Failed to load focussed bitmap of bitmap button\n");
			return bAllLoaded=FALSE;
		}
	}
	
	if (lpszBitmapDisabled!=NULL)
	{
		if (!m_bitmapDisabled.LoadBitmap(lpszBitmapDisabled))
		{
			TRACE0("Failed to load disabled bitmap of bitmap button\n");
			return bAllLoaded=FALSE;
		}
	}
	return bAllLoaded;
}
